package main

import (
	"dos_framework/es"
	"fmt"
	"io"
	"log"
	"net/http"
	"os"
	"path/filepath"
	"strings"
)

func main() {
	files, _ := filepath.Glob(os.Getenv("STORAGE_ROOT") + "/objects/*")
	for i := range files {
		hash := strings.Split(filepath.Base(files[i]), ".")[0]
		hashInMetadata, e := es.HasHash(hash)
		if e != nil {
			log.Panicln(e)
			return
		}
		if !hashInMetadata {
			//log.Println("delete", hash)
			del(hash)
		}
	}
}

func del(hash string) {
	log.Println("delete", hash)
	url := "http://" + os.Getenv("LISTEN_ADDRESS") + "/objects/" + hash
	req, _ := http.NewRequest("DELETE", url, nil)
	fmt.Println("url:", url)
	client := &http.Client{}

	r, e := client.Do(req)
	if e != nil {
		log.Println(e)
		return
	}
	b, _ := io.ReadAll(r.Body)
	fmt.Printf("del results: %d, %s\n", r.StatusCode, string(b))
}
